package com.senven.common.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.senven.common.entity.Role;
import com.senven.common.entity.User;
import com.senven.common.user.feign.RoleFeign;
import com.senven.common.user.vo.UserVo;
import com.senven.common.utils.WebUtils;
import com.senven.common.vo.CommonResult;
import org.springframework.data.redis.core.RedisTemplate;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;
import com.senven.common.user.service.UserService;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/user/user")
@Api(tags = "用户传给前端的所有接口")
public class UserController {
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private UserService userService;
    @Autowired
    private RoleFeign roleFeign;

    @ApiOperation(value = "查询全部用户的功能接口")
    @RequestMapping(value = "/findInfo")
    public CommonResult findInfo( Integer currentPage, Integer pageSize, @RequestBody UserVo userVo) {
        System.out.println(pageSize + "+===" + currentPage + "=======" + userVo);
        CommonResult info = userService.findInfo(currentPage, pageSize, userVo);
        System.out.println(info);
        return info;
    }

    @ApiOperation(value = "添加用户的功能")
    @RequestMapping(value = "/insertUser")
    public CommonResult insertUser(@RequestBody User user) {
        return userService.insertUser(user);
    }

    @ApiOperation(value = "修改用户的功能")
    @RequestMapping(value = "/updateUser")
    public CommonResult updateUser(@RequestBody User user) {
        return userService.updateUser(user);
    }

    @ApiOperation(value = "删除用户的功能")
    @RequestMapping(value = "/deleteUser")
    public CommonResult deleteUser( String id) {
        return userService.deleteUser(id);
    }

    @RequestMapping(value = "/findAllRole")
    @ApiOperation(value = "通过role微服务调用的查询全部role的接口")
    public CommonResult findAllRole() {
        List<Role> allRole = roleFeign.findAllRole();
        return new CommonResult(2000, "查找全部角色成功", allRole);
    }

    @ApiOperation("test")
    @RequestMapping(value = "/test")
    public int test(Role role) {
        int i = roleFeign.updateRole(role);
        return i;
    }

    @ApiOperation(value = "拿到登录者的头像")
    @PostMapping(value = "/loginAvater")
    public CommonResult loginAvater(HttpServletRequest request) {

        String token = request.getHeader("token");
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        String url = (String) user.get("url");
        return new CommonResult(2000, "查询成功", url);

    }

    @ApiOperation(value = "拿到登录者的信息")
    @PostMapping("/getInfo")
    public CommonResult getInfo(HttpServletRequest request) {
        String token = request.getHeader("token");
        String s = redisTemplate.opsForValue().get(token);
        Map map = JSON.parseObject(s, Map.class);
        JSONObject user = (JSONObject) map.get("user");
        return new CommonResult(2000, "查询成功", user);

    }
    @ApiOperation(value = "查询用户的名字")
    @GetMapping(value = "/findUsername")
    public CommonResult findUsername(){
        return userService.findUsername();
    }
    @ApiOperation(value = "修改密码")
    @PostMapping(value = "/updatePassword")
    public CommonResult updatePassword(HttpServletRequest request,@RequestBody Map map){
        return userService.updatePassword(request,map);
    }


}
